package com.hunan.chinamobile.manager;


import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunan.chinamobile.core.BaseController;
import com.hunan.chinamobile.core.Result;
import com.hunan.chinamobile.core.ResultGenerator;
import com.hunan.chinamobile.dto.BaseStationDTO;
import com.hunan.chinamobile.model.BaseStation;
import com.hunan.chinamobile.model.Device;
import com.hunan.chinamobile.service.BaseStationService;
import com.hunan.chinamobile.util.BaiduUtil;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/base_station")
public class BaseStationController  extends BaseController {

    @Resource
    private BaseStationService baseStationService;

    @PostMapping
    public Result add(@RequestBody BaseStation baseStation){
        if(baseStation == null || StringUtils.isEmpty(baseStation.getBaseName()))
            return ResultGenerator.genFailResult("参数为空！");
        if(baseStationService.check(baseStation.getBaseName()) != null)
            return ResultGenerator.genFailResult("该基站已存在！");
        baseStation.setId(UUID.randomUUID().toString());
        if(!StringUtils.isEmpty(baseStation.getAddress())){
            Map<String,Double> pointMap = BaiduUtil.getPoint(baseStation.getAddress());
            baseStation.setLat(pointMap.get("lat"));
            baseStation.setLng(pointMap.get("lng"));
        }
        baseStation.setCreatedAt(new Date());
        baseStation.setUpdatedAt(new Date());
        baseStationService.save(baseStation);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    public Result update(@RequestBody BaseStation baseStation){
        if(baseStation == null || StringUtils.isEmpty(baseStation.getBaseName()))
            return ResultGenerator.genFailResult("参数为空！");
        BaseStation bs = baseStationService.check(baseStation.getBaseName());
        if(bs != null && !bs.getId().equals(baseStation.getId())){
            return ResultGenerator.genFailResult("基站名称冲突！");
        }
        if(!StringUtils.isEmpty(baseStation.getAddress())){
            Map<String,Double> pointMap = BaiduUtil.getPoint(baseStation.getAddress());
            baseStation.setLat(pointMap.get("lat"));
            baseStation.setLng(pointMap.get("lng"));
        }
        baseStation.setUpdatedAt(new Date());
        baseStationService.update(baseStation);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable String id){
        baseStationService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping
    public Result list(HttpServletRequest request, HttpServletResponse response,
                       @RequestParam(required = false) String cityId,
                       @RequestParam(required = false) String bsName,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "size", defaultValue = "20") Integer size){
        if(StringUtils.isEmpty(cityId)){
            cityId = getCityId(request, response);
        }
        if(!StringUtils.isEmpty(bsName) && bsName.equals("%")) bsName="\\u0025";
        PageHelper.startPage(page,size);
        List<BaseStationDTO> dtos = baseStationService.findList(cityId, bsName);
        PageInfo<BaseStationDTO> pageInfo = new PageInfo<>(dtos);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/all")
    public Result getAll(HttpServletRequest request, HttpServletResponse response,
                         @RequestParam(required = false)String cityId){
        if(StringUtils.isEmpty(cityId)){
            cityId = getCityId(request, response);
        }
        if(StringUtils.isEmpty(cityId)){
            return ResultGenerator.genFailResult("cityId参数不能为空");
        }
        List<BaseStation> bss = baseStationService.findByCityId(cityId);
        return ResultGenerator.genSuccessResult(bss);
    }
}
